<template>
  <div class="findPassword">
    <div class="reg_header">
      <div class="main">
        <!-- <a :href="url">
                    <img src="@/assets/img/WechatIMG233.png" alt />
                </a>-->
      </div>
    </div>
    <div class="logo">
      <a :href="url">
        <img src="@/assets/img/zhaopinLogo.png" alt />
      </a>
    </div>
    <div class="password_box" @keyup.enter="onSubmit">
      <h2>
        <span>忘记密码</span>
        <router-link to="/login">返回</router-link>
      </h2>
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item prop="account">
          <el-input v-model.trim="form.account" placeholder="请输入手机号码或邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="captchaCode">
          <el-input class="captchaCode" v-model="form.captchaCode" placeholder="图片验证码"></el-input>
          <div class="img_box">
            <img :src="img" alt @click="changeImage" />
          </div>
        </el-form-item>
        <el-form-item prop="smsCode" class="btn_inner">
          <el-input v-model="form.smsCode" placeholder="手机或邮箱验证码">
            <el-button slot="append" type="text" @click="postMassage" :class="{'disabled':countDown}">
              发送验证码
              <span v-show="countDown">({{countDown}})</span>
            </el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请确认密码" show-password></el-input>
        </el-form-item>
        <el-form-item prop="comfirmPassword">
          <el-input v-model="form.comfirmPassword" placeholder="请确认密码" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="submit_btn" type="primary" @click="onSubmit">确认</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { captchaImg, postMsg, findPassword, mailCode } from '../../../api/index';
import { encrypt } from '../../../utils/unitFile';

export default {
  name: 'findPassword',
  data () {
    var checkImage = (rule, value, callback) => {
      console.log('图片');
      if (value.length > 3) {
        captchaImg({ code: this.form.captchaCode, uuid: this.form.captchaUUid }).then(data => {
          if (data.data) {
            this.checkImage = true;
            callback();
          } else {
            callback(new Error('请输入正确验证码'));
          }
        });
      } else {
        callback();
      }
    };
    var validateComfirmPwd = (rule, value, callback) => {
      console.log(this.form.password, value);
      if (this.form.password != value) {
        callback(new Error('确认密码与新密码不一致！'));
      } else {
        callback();
      }
    };
    return {
      timerMin: null,
      timer: null,
      url: '',
      img: '',
      checkImage: false,
      countDown: '',
      clickTime: 0,
      form: {
        comfirmPassword: '',
        account: '',
        password: '',
        captchaCode: '',
        captchaUUid: '',
        smsCode: '',
        selected: false
      },
      rules: {
        account: [
          { required: true, message: '请输入手机号或邮箱', trigger: 'blur' },
          {
            pattern: /^(1[3456789]\d{9})|([A-Za-z0-9]+([_\.A-Za-z0-9\-]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6})$/,
            message: '请输入正确的手机号或邮箱',
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^[0-9a-zA-Z_]{6,20}$/, message: '密码由长度为6-20的数字、字母或下划线组成' }
        ],
        captchaCode: [{ required: true, message: '请输入图片验证码', trigger: 'blur' }, { validator: checkImage }],
        smsCode: [{ required: true, message: '请输入手机或邮箱验证码', trigger: 'blur' }],
        comfirmPassword: [{ required: true, message: '请确认密码', trigger: 'blur' }, { validator: validateComfirmPwd }]
      }
    };
  },
  mounted () {
    this.changeImage();
    this.url = process.env.VUE_APP_URL;
  },
  methods: {
    // 提交
    onSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          let query = {
            account: this.form.account,
            password: encrypt(this.form.password),
            captchaCode: this.form.captchaCode,
            captchaUUid: this.form.captchaUUid,
            code: this.form.smsCode,
            captchaChannelType: 3
          };
          findPassword(query).then(data => {
            console.log(data);
            if (data.success) {
              this.$message.success('修改成功');
              if (this.checked) {
                // localStorage.setItem('password', data.password);
              }
              this.$router.push({ path: '/login' });
            } else {
              this.changeImage();
              this.checkImage = false;
              this.clickTime = 0;
              this.countDown = '';
              this.form.smsCode = '';
              clearInterval(this.timerMin);
              clearTimeout(this.timer);
              this.$message.error(`${data.msg}`);
            }
          });
        } else {
          // this.$message.error('请输入账号和密码');
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 发送验证码
    postMassage () {
      if (this.form.account) {
        if (this.checkImage) {
          if (this.countDown) {
            return;
          }
          let checkAccount = this.form.account.includes("@");
          let query = {
            uuid: this.form.captchaUUid,
            code: this.form.captchaCode,
            type: 3
          };
          checkAccount ? query['mail'] = this.form.account : query['phone'] = this.form.account;
          (checkAccount ? mailCode(query) : postMsg(query)).then(data => {
            if (data.success) {
              let previous = this.clickTime;
              let now = new Date();
              if (now - previous > 60000) {
                console.log(1);
                this.clickTime = new Date();
                this.countDown = 60;
                this.timerMin = setInterval(() => {
                  this.countDown -= 1;
                }, 1000);
                this.timer = setTimeout(() => {
                  this.countDown = '';
                  clearInterval(this.timerMin);
                  clearTimeout(this.timer);
                }, 60000);
              }
            } else {
              this.checkImage = false;
              this.$message.error(`${data.msg}`);
            }
          });
        } else {
          this.$message.error('请正确输入图片验证码');
        }
      } else {
        this.$message.error('请输入手机号');
      }
    },
   
  }
};
</script>

<style lang="css" scoped>
.logo {
    margin-top: 30px;
    text-align: center;
    cursor: pointer;
}
.findPassword {
    width: 100%;
    background: rgba(246, 246, 248, 1);
}
.reg_header {
    width: 100%;
    height: 40px;
    background: rgba(24, 147, 252, 1);
    opacity: 1;
    margin-bottom: 40px;
}
.el-input-group__append /deep/ .el-button.disabled {
    color: rgba(117, 117, 117, 1);
}
.main a {
    display: inline-block;
    height: 40px;
}
.main a img {
    margin-top: 5px;
}
.password_box {
    width: 540px;
    background: rgba(255, 255, 255, 1);
    opacity: 1;
    margin: 60px auto 0;
    padding: 61px 83px 25px;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -ms-box-sizing: border-box;
    -o-box-sizing: border-box;
}
.password_box h2 {
    height: 28px;
    font-size: 20px;
    font-family: PingFang SC;
    font-weight: 600;
    line-height: 28px;
    color: rgba(34, 34, 34, 1);
    opacity: 1;
    margin-bottom: 38px;
}
.password_box h2 a {
    float: right;
    width: 40px;
    height: 28px;
    font-size: 20px;
    font-family: PingFang SC;
    font-weight: 600;
    line-height: 22px;
    color: rgba(24, 147, 252, 1);
    opacity: 1;
}
.password_box .el-form-item {
    margin-bottom: 36px;
}
.el-form-item .el-input {
    line-height: 44px;
}
.el-form-item .captchaCode {
    width: 255px;
    display: inline-block;
    vertical-align: top;
    margin-right: 12px;
}

.el-form-item .captchaCode /deep/ input.el-input__inner {
    width: 255px;
    text-transform: uppercase;
}
.el-form-item.btn_inner /deep/ .el-input__inner {
    width: 100%;
}
.password_box .btn_inner /deep/ .el-input-group__append {
    text-align: center;
    width: 106px;
    padding: 0;
}
.password_box .btn_inner /deep/ .el-button {
    width: 110px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    vertical-align: middle;
    background: #f5f7fa;
    color: rgba(0, 147, 255, 1);
    opacity: 1;
    padding: 0;
    padding-left: 6px;
    cursor: pointer;
    border: none;
    /* border-left: 2px solid rgba(232, 232, 232, 1); */
}
.password_box .btn_inner /deep/ .el-button.disabled {
    color: #999999;
}
.el-form-item {
    height: 44px;
}
.el-form-item .el-form-item__content .img_box {
    display: inline-block;
    width: 106px;
    height: 44px;
    background: rgba(248, 248, 248, 1);
    opacity: 1;
    border-radius: 4px;
}
.el-form-item .el-form-item__content .img_box img {
    width: 100%;
    height: 100%;
    cursor: pointer;
}
.el-form-item /deep/ .el-input__inner {
    width: 373px;
    height: 44px;
    background: rgba(248, 248, 248, 1);
    opacity: 1;
    border-radius: 4px;
}
.el-form-item .submit_btn {
    width: 373px;
    height: 44px;
    background: rgba(24, 147, 252, 1);
    opacity: 1;
    border-radius: 4px;
    font-size: 16px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 22px;
    color: rgba(255, 255, 255, 1);
}
</style>